package com.ming.transaction.controller;
import com.ming.transaction.common.Result;
import com.ming.transaction.common.annotation.RecordLoggerAnnotation;
import com.ming.transaction.entity.Goods;
import com.ming.transaction.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈仕健
 * @since 2023-05-11
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;

    /**
     * 管理员商品分页查询
     * @param pageNum
     * @param pageSize
     * @param gname
     * @param classly
     * @return
     */
    @ApiOperation("商品分页查询接口")
    @GetMapping("/page")
    public Result PageSearch(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "") String gname,
                             @RequestParam(defaultValue = "") String classly){
        return goodsService.pageSearch(pageNum,pageSize,gname,classly);
    }


    /**
     * 用户分页查询商品
     * @param pageNum
     * @param pageSize
     * @param gname
     * @param classly
     * @param unumber
     * @return
     */
    @ApiOperation("用户商品分页查询接口")
    @GetMapping("/pageu")
    public Result PageSearch(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "") String gname,
                             @RequestParam(defaultValue = "") String classly,
                             @RequestParam String unumber){
        return goodsService.pageSearchs(pageNum,pageSize,gname,classly,unumber);
    }


    /**
     * 修改商品信息
     * @param goods
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品信息")
    @RecordLoggerAnnotation(value = "修改商品信息")
    public Result upGoods(@RequestBody Goods goods){
        return goodsService.goodupdate(goods);
    }

    /**
     * 商品下架
     * @param id
     * @return
     */
    @ApiOperation("商品下架")
    @PutMapping("/{id}")
    public Result upStuGoods(@PathVariable("id") Integer id){
       return  goodsService.upStugoods(id);
    }

    /**
     * 用户删除商品
     * @param id
     * @return
     */
    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    @RecordLoggerAnnotation(value = "删除商品")
    public Result delGoods(@PathVariable Integer id){
        return goodsService.deleteById(id);
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    @ApiOperation("批量删除商品")
    @RecordLoggerAnnotation(value = "批量删除商品")
    public Result delsGoods(@RequestBody List<Integer> ids){
        return goodsService.deleteByIds(ids);
    }

    /**
     * 存储商品
     * @param goods
     * @return
     */
    @ApiOperation("保存商品")
    @PostMapping
    @RecordLoggerAnnotation(value = "存储商品")
    public Result saveGoods(@RequestBody Goods goods){
        return goodsService.saveGoods(goods);
    }

    /**
     * 商品头像上传接口
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("商品头像上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) throws IOException {
        return goodsService.uploadImage(file);
    }

    /**
     * 用户头像加载接口
     * @param fileUUID
     * @throws IOException
     */
    @ApiOperation("用户头像获取")
    @GetMapping(value = "/load/{fileUUID}",produces = "image/jpeg")
    public void download(@PathVariable String fileUUID) throws IOException {
        goodsService.dowmloadImage(fileUUID);
    }

    @ApiOperation("商品列表获取")
    @GetMapping("/list")
    public List<Goods> findAllGoods(){
        return goodsService.list();
    }
}

